﻿@{
    ViewBag.Title = "MenuEdit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model CGT.Infrastructure.Entity.GovMenu

@section style{
<style>
    .form-horizontal .control-label {
        width: 30%;
        float: left;
        text-align: right;
    }

    .col-sm-3 {
        width: 70%;
        float: left;
    }
</style>
<link href="@Url.Content("~/css/plat/blue.css")" rel="stylesheet">
}
<form id="form1" class="form-horizontal">
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box-body">

                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">父级：</label>
                        <div class="col-sm-3">
                            <select class="form-control" style="width:300px;" name="selParentId" id="selParentId"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">所属类别：</label>
                        <div class="col-sm-3">
                            <select class="form-control" style="width:300px;" name="drpCategory" id="drpCategory"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">仅允许警员操作：</label>
                        <div class="col-sm-3">
                            @if (Model.OnlyForPolice == 1)
                            {
                            <label class="radio-inline">
                                <input type="radio" name="drpOnlyPolice" value="1" checked="checked"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="drpOnlyPolice" value="0"> 否
                            </label>
                            }
                            else
                            {
                            <label class="radio-inline">
                                <input type="radio" name="drpOnlyPolice" value="1"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="drpOnlyPolice" value="0" checked="checked"> 否
                            </label>
                            }
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">是否核心功能：</label>
                        <div class="col-sm-3">
                            @if (Model.IsCoreFunction == 1)
                            {
                            <label class="radio-inline">
                                <input type="radio" name="drpIsCoreFunction" value="1" checked="checked"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="drpIsCoreFunction" value="0"> 否
                            </label>
                            }
                            else
                            {
                            <label class="radio-inline">
                                <input type="radio" name="drpIsCoreFunction" value="1"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="drpIsCoreFunction" value="0" checked="checked"> 否
                            </label>
                            }

                        </div>
                    </div>
                    <div class="form-group">
                        <label for="name" class="col-sm-2 control-label">是否常规业务：</label>
                        <div class="col-sm-3">
                            @if (Model.IsConventional == 1)
                            {
                            <label class="radio-inline">
                                <input type="radio" name="drpIsConventional" value="1" checked="checked"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="drpIsConventional" value="0"> 否
                            </label>
                            }
                            else
                            {
                            <label class="radio-inline">
                                <input type="radio" name="drpIsConventional" value="1"> 是
                            </label>
                            <label class="radio-inline">
                                <input type="radio" name="drpIsConventional" value="0" checked="checked"> 否
                            </label>
                            }
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">名称：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuName" style="width:300px;" name="txtName" maxlength="50" id="txtName" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">URL：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuUrl" style="width:300px;" name="txtURL" maxlength="500" id="txtURL" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">代码：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.MenuCode" style="width:300px;" name="txtMenuCode" maxlength="16" id="txtMenuCode" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">排序：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.OrderNum" style="width:300px;" name="txtOrderNum" maxlength="5" id="txtOrderNum" />
                        </div>
                    </div>
                </div>
                <div class="box-footer text-center">
                    <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
                    <button class="btn" type="button" id="cancel">取 消 </button>
                    <input type="hidden" id="MenuID" name="MenuID" value="@Model.MenuID" />
                </div>


            </div>

        </div>
        <!-- /.row -->
    </section>
</form>
@section scriptsBottom{
<script src="@Url.Content("~/js/icheck.min.js")"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('input').iCheck({
            checkboxClass: 'icheckbox_flat-blue',
            radioClass: 'iradio_flat-blue'
        });

        var rTypeId = @Model.RoleTypeID;
        initRoleType(rTypeId);
        initParentMenu();
        initFormValidate();

        $("#cancel").click(function () {
            BMCW.closeTopLayer();
        });
        $("#btnSave").click(function () {
            save();
        });
    });

    //父级菜单
    function initParentMenu() {
        var pMenuId = @Model.ParentID;
        var pId = @ViewBag.Pid;
        var mid = @Model.MenuID;
        $("#selParentId").append("<option value='0'>一级菜单</option>");
        if((mid > 0 && pMenuId == 0)||pId > 0||(mid == 0 && pMenuId == 0 && pId == 0)){
            $("#selParentId").attr("disabled", "disabled");
        }
        if (pMenuId == 0&& pId==0) {
            return;
        }
        var url = '@Url.Action("GetParentMenuList", "Authority")';
        BMCW.ajax({
            url: url,
            type: "GET",
            success: function (o) {
                if (o.status == 0) {
                    $.each(o.data, function (i) {
                        var sel = "";
                        if(pId > 0)
                        {
                            if (o.data[i].MenuID==pId) {
                                sel = "selected='true'";
                            }
                        }else{
                            if (o.data[i].MenuID == pMenuId) {
                                sel = "selected='true'";
                            }
                        }
                        $("#selParentId").append("<option value='" + o.data[i].MenuID + "' "+sel+">" + o.data[i].MenuName + "</option>");
                    });
                }
            }
        });
    }

    //所属类别
    function initRoleType(typeId) {
        $("#drpCategory").append("<option value=''>请选择...</option>");
        // $("#drpCategory").attr("disabled", "disabled");
        var url = '@Url.Action("GetRoleTypeList", "Authority")';
        BMCW.ajax({
            url: url,
            type: "GET",
            success: function (o) {
                if (o.status == 0) {
                    $.each(o.data, function (i) {
                        var sel = "";
                        if (o.data[i].RoleTypeID == typeId) {
                            sel = "selected='true'";
                        }
                        $("#drpCategory").append("<option value='" + o.data[i].RoleTypeID + "' "+sel+">" + o.data[i].TypeName + "</option>");
                    });
                }
            }
        });
    }

    //保存
    function save()
    {
        var bootstrapValidator = $("#form1").data('bootstrapValidator');
        bootstrapValidator.validate();

        if (bootstrapValidator.isValid()) {
            var url = '@Url.Action("SaveMenu", "Authority")';
            var arrData = $("#form1").serializeArray();
            var data = {};
            for (var i = 0; i < arrData.length; i++) {
                data[arrData[i].name] = arrData[i].value;
            }
            if (data.selParentId==null) {
                data["selParentId"] = $("#selParentId").val();
            }
            BMCW.ajax({
                url: url,
                type: "POST",
                data: data,
                success: function (o) {
                    if (o.status == 0) {
                        BMCW.msg("保存成功",function(){
                            BMCW.getTopSelectedWin().initData();
                        });
                    } else {
                        BMCW.alert("保存失败");
                    }
                }
            });
        }
    }

    //验证
    function initFormValidate() {
        $("#form1").bootstrapValidator({
            submitHandler: function (valiadtor, loginForm, submitButton) {
                valiadtor.defaultSubmit();
            },
            fields: {
                txtName: {
                    validators: {
                        notEmpty: {
                            message: '菜单名称不能为空'
                        },
                        stringLength: {
                            /*长度提示*/
                            min: 1,
                            max: 16,
                            message: '菜单名称长度必须在1到16之间'
                        }
                    }
                },
                txtURL: {
                    validators: {
                        notEmpty: {
                            message: 'URL不能为空'
                        },
                        stringLength: {
                            /*长度提示*/
                            min: 1,
                            max: 500,
                            message: 'URL长度必须在1到500之间'
                        }
                    }
                },
                drpCategory: {
                    validators: {
                        notEmpty: {
                            message: '请选择所属类别'
                        }
                    }
                },
                txtOrderNum: {
                    validators: {
                        notEmpty: {
                            message: '排序不能为空'
                        },
                        digits: {
                            message: '排序只能是数字'
                        }
                    }
                },
                txtMenuCode: {
                    validators: {
                        notEmpty: {
                            message: '代码不能为空'
                        },
                        stringLength: {
                            /*长度提示*/
                            min: 1,
                            max: 12,
                            message: '代码长度必须在1到12之间'
                        },
                        callback: {
                            message: '代码重复',
                            callback: function (value, validator, $field) {
                                var bol = true;
                                $.ajax({
                                    url: '@Url.Action("IsExistCode", "Authority")',
                                    type: 'GET',
                                    async: false,
                                    dataType: "json",
                                    data: {
                                        code: $("#txtMenuCode").val(),
                                        mid: @Model.MenuID,
                                    }

                                }).done(function (data) {
                                    //刷新
                                    bol = data.status==-1?true:false;

                                }).fail(function (data) {
                                    bol = true;
                                });
                                return bol;
                            }
                        }
                    }

                }

            }
        });
    }

</script>
}
